*********************************************
* Create Main Figures in Article + Appendix Figures
*********************************************
clear all
global PATH  ""
global DIR "$PATH\tables_appendix"
global OUTPUT "$PATH\figures_appendix"



********************************************************************************
********************************************************************************
********************************************************************************
** Fig A.1 - Roubstness to treatment def
********************************************************************************
********************************************************************************
********************************************************************************

**************************
**  panel A - base 2012
**************************
*set number of specs in table 
local ss = 1
local sp = `ss'+1
* set rows - if base 2012: sr = 27 & srr=1 ;  if base 2011,2012: sr = 25 & srr=2
local sr = 27
local srr = 1

* import csv file and select first column only 
import delimited "$DIR\figure_A1_panelA_itt.csv", rowrange(4:`sr') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

set obs `=_N+1'
replace year = 2012 if year == .
sort year 

gen vv=`srr'
if vv==2 {
set obs `=_N+1'
replace year = 2011 if year == .
sort year 
}
drop vv

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "itt"
tempfile titt
save `titt', replace


*set number of specs in table 
local ss = 1
local sp = `ss'+1

* set rows - if base 2012: sr = 27 & srr=1 ;  if base 2011,2012: sr = 25 & srr=2
local sr = 27
local srr = 1

* import csv file and select first column only 
import delimited "$DIR\figure_A1_panelA_2sls.csv", rowrange(4:`sr') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]	
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

set obs `=_N+1'
replace year = 2012 if year == .
sort year 

gen vv=`srr'
if vv==2 {
set obs `=_N+1'
replace year = 2011 if year == .
sort year 
}
drop vv

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "iv"
append using `titt'

drop year1 year2
gen year1 = year-.05
gen year2 = year+.05

local lcolor1 "navy"
local secolor1 "gs8"
local lcolor2 "cranberry"
local secolor2 "gs8"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec = 1
local spec = `zspec'+1

local gtype "scatter"

graph twoway (rcap lower`spec' upper`spec' `xvar'1  if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec' `xvar'1 if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) ///
			(rcap lower`spec' upper`spec' `xvar'2  if year>=`xvarmin' & year<=`xvarmax' & tt=="iv",  lcolor(`secolor2') ) ///
			(`gtype' coeff`spec' `xvar'2 if year>=`xvarmin' & year<=`xvarmax'  & tt=="iv",  lcolor(`lcolor2') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor2') ) , ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			 xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))  ytitle("")  xtitle("year") ///
			 yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ylab(`ymin'(`ygap')`ymax', nogrid)   ///
			 legend(order(2 "ITT" 4 "2sls"  )) ///
	       graphregion(color(white)) bgcolor(white)  ///
           ysize(7) xsize(10)
graph export $OUTPUT/fig_A1_panelA.png, replace

		   
**************************
**  panel B - base 2010-2012
**************************
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 21

* import csv file and select first column only 
import delimited "$DIR\figure_A1_panelB_itt.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+3'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
replace year = 2010 if zz==1 & zz7==3
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen tt = "itt"
tempfile titt
save `titt', replace


local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 21

* import csv file and select first column only 
import delimited "$DIR\figure_A1_panelB_2sls.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]	
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+3'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
replace year = 2010 if zz==1 & zz7==3
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen tt = "iv"
append using `titt'

gen year1 = year-.05
gen year2 = year+.05

local lcolor1 "navy"
local secolor1 "gs8"
local lcolor2 "cranberry"
local secolor2 "gs8"
local ymin = -.03
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec = 1
local spec = `zspec'+1

local gtype "scatter"

graph twoway (rcap lower`spec' upper`spec' `xvar'1  if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec' `xvar'1 if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) ///
			(rcap lower`spec' upper`spec' `xvar'2  if year>=`xvarmin' & year<=`xvarmax' & tt=="iv",  lcolor(`secolor2') ) ///
			(`gtype' coeff`spec' `xvar'2 if year>=`xvarmin' & year<=`xvarmax'  & tt=="iv",  lcolor(`lcolor2') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor2') ) , ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			 xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))  ytitle("")  xtitle("year") ///
			 yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ylab(`ymin'(`ygap')`ymax', nogrid)   ///
			 legend(order(2 "ITT" 4 "2sls"  )) ///
	       graphregion(color(white)) bgcolor(white)  ///
           ysize(7) xsize(10)
graph export $OUTPUT/fig_A1_panelB.png, replace



		
		
**************************************************************************************
**************************************************************************************
**************************************************************************************
**  Figure A.2 - Robustness to controls and subsamples
**************************************************************************************
**************************************************************************************
**************************************************************************************

**************************
**  panel A - Robustness - controls
**************************
*set number of specs in table 
local ss = 4
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A2_panelA.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year+.1
gen year2 = year+.025
gen year3 = year-.05
gen year4 = year-.125

gen tt = "itt"
tempfile titt
save `titt', replace

local lcolor1 "navy"
local secolor1 "navy"
local lcolor2 "cranberry"
local secolor2 "cranberry"
local lcolor3 "green"
local secolor3 "green"
local lcolor4 "gold"
local secolor4 "gold"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "4"
local spec1_1 = `zspec1_1'+1
local spec1_2 = `zspec1_1'+2
local spec1_3 = `zspec1_1'+3
local zspec2 "3"
local spec2 = `zspec2'+1
local zspec3 "2"
local spec3 = `zspec3'+1
local zspec4 "1"
local spec4 = `zspec4'+1

local gtype "scatter"
*
* Event study figure *
*
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'1  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1') lpattern(dash) ) ///
			(`gtype' coeff`spec1_1' `xvar'1 if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) ///
			(rcap lower`spec2' upper`spec2' `xvar'2  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor2') lpattern(dash) ) ///
			(`gtype' coeff`spec2' `xvar'2 if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor2') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor2') ) ///
			(rcap lower`spec3' upper`spec3' `xvar'3  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor3') lpattern(dash) ) ///
			(`gtype' coeff`spec3' `xvar'3 if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor3') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor3') ) ///
			(rcap lower`spec4' upper`spec4' `xvar'4  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor4') lpattern(dash) ) ///
			(`gtype' coeff`spec4' `xvar'4 if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor4') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor4') ), ///
			graphregion(fcolor(white) icolor(white) color(white))    xtitle("year")     ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(8 "net inc only") label(6 "+ val added") label(4 "+ industry") label(2 "+ CZ (main)") order(8 6 4 2) col(1))		
graph export $OUTPUT/fig_A2_panelA.png, replace

			
			

*********************************************************************************
**  Panel B - Robustness against capital income - top 0.1%, wage majority, capital income minority 
*********************************************************************************
************************
**  robustness - no top 0.01
************************
local sss "top01"

*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A2_panelB_top01.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]	
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "`sss'"
tempfile `sss'
save ``sss'', replace


*******************************************************
**  robustness - wage majority
*******************************************************
local sss "wage"

*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A2_panelB_wagemax.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]	
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "`sss'"
tempfile `sss'
save ``sss'', replace



*******************************************************
**  robustness - capital income minority
*******************************************************
local sss "cap"

*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A2_panelB_capinc2max.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]	
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "`sss'"
tempfile `sss'
save ``sss'', replace



*******************************************************
**   robustness - investment income minority
*******************************************************
local sss "invest"

*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A2_panelB_invest2max.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "`sss'"
tempfile `sss'
save ``sss'', replace

append using `cap'
append using `wage'
append using `top01' 

drop year1 year2 year3
gen year1 = year+.1
gen year2 = year+.025
gen year3 = year-.05
gen year4 = year-.125

local ts1 "top01"
local ts2 "wage"
local ts3 "cap"
local ts4 "invest"

local lcolor1 "navy"
local secolor1 "navy"
local lcolor2 "cranberry"
local secolor2 "cranberry"
local lcolor3 "green"
local secolor3 "green"
local lcolor4 "gold"
local secolor4 "gold"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "1"
local spec1_1 = `zspec1_1'+1
local spec1_2 = `zspec1_1'+2
local spec1_3 = `zspec1_1'+3
local zspec2 "1"
local spec2 = `zspec2'+1
local zspec3 "1"
local spec3 = `zspec3'+1
local zspec4 "1"
local spec4 = `zspec4'+1

local gtype "scatter"
*
* Event study figure *
*
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'1  if year>=`xvarmin' & year<=`xvarmax' & tt=="`ts1'",  lcolor(`secolor1') lpattern(dash) ) ///
			(`gtype' coeff`spec1_1' `xvar'1 if year>=`xvarmin' & year<=`xvarmax' &  tt=="`ts1'",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) ///
			(rcap lower`spec2' upper`spec2' `xvar'2  if year>=`xvarmin' & year<=`xvarmax'  & tt=="`ts2'",  lcolor(`secolor2') lpattern(dash) ) ///
			(`gtype' coeff`spec2' `xvar'2 if year>=`xvarmin' & year<=`xvarmax' &  tt=="`ts2'",  lcolor(`lcolor2') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor2') ) ///
			(rcap lower`spec3' upper`spec3' `xvar'3  if year>=`xvarmin' & year<=`xvarmax'  & tt=="`ts3'",  lcolor(`secolor3') lpattern(dash) ) ///
			(`gtype' coeff`spec3' `xvar'3 if year>=`xvarmin' & year<=`xvarmax'   & tt=="`ts3'",  lcolor(`lcolor3') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor3') ) ///
			(rcap lower`spec4' upper`spec4' `xvar'4  if year>=`xvarmin' & year<=`xvarmax' &  tt=="`ts4'",  lcolor(`secolor4') lpattern(dash) ) ///
			(`gtype' coeff`spec4' `xvar'4 if year>=`xvarmin' & year<=`xvarmax' &  tt=="`ts4'",  lcolor(`lcolor4') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor4') ), ///
			graphregion(fcolor(white) icolor(white) color(white))    xtitle("year")     ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(8 "investment inc minority") label(6 "capital inc minority") label(4 "wages majority") label(2 "exclude top 0.1%") order(2 4 6 8) )			
graph export $OUTPUT/fig_A2_panelB.png, replace
			
			

			
			
********************************************************************************
********************************************************************************
********************************************************************************
**  Figure A.3 - Single owner firm main results
********************************************************************************
********************************************************************************
********************************************************************************

local sp "2"
******************
**  Panel A - trends
******************
import delimited "$DIR\figure_A3_panelA_ctrl.csv", rowrange(28:51) colrange(:2) clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

**For base 2012
set obs `=_N+1'
replace year = 2012 if year == .
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

ren coeff`sp' coeffctrl 
drop ste* upper* lower*

sort year
tempfile tctrl
save `tctrl', replace


******************
**  Panels B and D - ITT and 2sls DD
******************
*set number of specs in table 
local ss = 4
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A3_panelB.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1
gen tt = "itt"

tempfile titt
save `titt', replace


******************
**  Panel D - 2sls DD
******************				
*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A3_panelD.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "iv"
append using `titt'

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "4"
local spec1_1 = `zspec1_1'+1

local gtype "scatter"
**
* Event study figure 
**	
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(gs8) ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			 ylabel(`ymin'(`ygap')`ymax') legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig_A3_panelB.png, replace 
	
	

/* Panel D */			
local lcolor1 "navy"
local secolor1 "gs8"
local lcolor2 "cranberry"
local secolor2 "gs8"
local ymin = -.03
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "4"
local spec1_1 = `zspec1_1'+1
local zspec2 "1"
local spec2 = `zspec2'+1

local gtype "scatter"

graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'3  if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(gs8)  ) ///
			(`gtype' coeff`spec1_1' `xvar'3 if year>=`xvarmin' & year<=`xvarmax'  & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) ///
			 (rcap lower`spec2' upper`spec2' `xvar'1  if year>=`xvarmin' & year<=`xvarmax' & tt=="iv",  lcolor(gs8)  ) ///
			(`gtype' coeff`spec2' `xvar'1 if year>=`xvarmin' & year<=`xvarmax' & tt=="iv" ,  lcolor(`lcolor2') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor2') ) , ///
			graphregion(fcolor(white) icolor(white) color(white))    ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' 2016)) 	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))  /// 
			legend(label(2 "ITT") label(4 "2sls") order(2 4))
graph export $OUTPUT/fig_A3_panelD.png, replace 
			


*****************
** panel A
*****************
sort year 
merge m:1 year using `tctrl'
drop _merge

local spec "4"
gen coefftreat = coeff`spec'+coeffctrl

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

graph twoway (connected coeffctrl `xvar'  if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(gs8) lwidth(medium) msymbol(o) msize(medium) mcolor(gs8) lpattern(dash) ) ///
			(connected coefftreat `xvar' if year>=`xvarmin' & year<=`xvarmax'   & tt=="itt",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "exposed") label(1 "unexposed") order(2 1))			///
			xtitle("year") 	
graph export $OUTPUT/fig_A3_panelA.png, replace 


******************
**  Panel C - first stage
******************
import delimited "$DIR\figure_A3_panelC.csv", clear
local tt "tt1112"
graph twoway (connected sh year if trt==1 & tvc=="`tt'", mcolor(navy) lcolor(navy) lpattern(solid)) /// 
			(connected sh year if trt==0 & tvc=="`tt'", mcolor(cranberry) lcolor(cranberry)  lpattern(dash)), /// 
			graphregion(fcolor(white) icolor(white) color(white))  plotregion(margin(medlarge)) ///
			ytitle("Fraction Treated") ///
			xline(2012.3, lcolor(black) lpattern(dash)) legend(col(2) order(1 "Basline Exposed Firms"  2 "Baseline Unexposed Firms"))
graph export "$OUTPUT/fig_A3_panelC.png", replace





********************************************************************************
********************************************************************************
********************************************************************************
**  Figure A.4 - netinc deciles
********************************************************************************
********************************************************************************
********************************************************************************
*  See Excel Spreadsheet fig6_new.xlsx in tables_main




********************************************************************************
********************************************************************************
********************************************************************************
**  Figure A.5 - robustness and placebo
********************************************************************************
********************************************************************************
********************************************************************************

*******************************************************
**  top v >250K
*******************************************************
local sss "tv250"
*set number of specs in table 
local ss = 4
local sp = `ss'+1

*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A5_top_v_top2.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "`sss'"
tempfile `sss'
save ``sss'', replace



*******************************************************
**  top v >350K
*******************************************************
local sss "tv350"
*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A5_top_v_top3.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "`sss'"
tempfile `sss'
save ``sss'', replace


*******************************************************
**  top v >250K
*******************************************************
local sss "t350v250"
*set number of specs in table 
local ss = 4
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A5.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]	
	drop temp3 coef1 coef2 
}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year
gen year2 = year+.1
gen year3 = year-.1

gen tt = "`sss'"
tempfile `sss'
save ``sss'', replace

append using `tv250'
append using `tv350'

local ts1 "tv250"
local ts2 "t350v250"
local ts3 "tv350"

local lcolor1 "navy"
local secolor1 "navy"
local lcolor2 "cranberry"
local secolor2 "cranberry"
local lcolor3 "green"
local secolor3 "green"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "4"
local spec1_1 = `zspec1_1'+1
local spec1_2 = `zspec1_1'+2
local spec1_3 = `zspec1_1'+3
local zspec2 "1"
local spec2 = `zspec2'+1
local zspec3 "4"
local spec3 = `zspec3'+1

local gtype "connected"
*
* Event study figure *
*
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' & tt=="`ts1'",  lcolor(`secolor1') lpattern(dash) ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' &  tt=="`ts1'",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) ///
			 (rcap lower`spec2' upper`spec2' `xvar'2  if year>=`xvarmin' & year<=`xvarmax' & tt=="`ts3'",  lcolor(`secolor3') lpattern(dash) ) ///
			(`gtype' coeff`spec2' `xvar'2 if year>=`xvarmin' & year<=`xvarmax' &  tt=="`ts3'",  lcolor(`lcolor3') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor3') ) ///
			(rcap lower`spec3' upper`spec3' `xvar'3  if year>=`xvarmin' & year<=`xvarmax'  & tt=="`ts2'",  lcolor(`secolor2') lpattern(dash) ) ///
			(`gtype' coeff`spec3' `xvar'3 if year>=`xvarmin' & year<=`xvarmax'   & tt=="`ts2'",  lcolor(`lcolor2') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor2') ), ///
			graphregion(fcolor(white) icolor(white) color(white))    xtitle("year")  ylab(`ymin'(`ygap')`ymax', nogrid)   ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "exposed v. >$200K") label(4 "exposed v. >$300K") label(6 "high income unexposed ($300-450K) v. >$200K") order(2 4 6) col(1))			
graph export $OUTPUT/fig_A5.png, replace


			
			

********************************************************************************
********************************************************************************
********************************************************************************
**  Figure A.6 - distribution decomp robustness
********************************************************************************
********************************************************************************
********************************************************************************

****************************
** Panel C - DD total 
****************************
* import csv file and select first column only 
import delimited $DIR\figure_A6_panelC.csv, rowrange(4:6) colrange(2:13) clear
gen year=2016

foreach i of numlist 1/12 {
	local j = `i'+1
	rename v`i' vv`j'
}

foreach i of numlist 2/13 {
	rename vv`i' v`i'
}	

foreach i of numlist 2/13 {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2
}
drop if coeff3 == .
keep year coeff* ste*

foreach i of numlist 2/13 {
	* generating upper and lower bound for CI
	gen upper`i' = coeff`i' + 1.96*ste`i' 
	gen lower`i' = coeff`i' - 1.96*ste`i' 
}

* reshape into long format
reshape long coeff ste lower upper, i(year) j(wagerange)	
gen status=1

* generating space in between bars for better visulation in the figure 
* create a status = 4 to make more room between each wage range 
set obs `=_N*2'
replace status = 4 if status==.
replace wagerange=2 if status==4
replace wagerange=wagerange[_n-1]+1 if status==4 & year[_n-1]==. & wagerange==2
sort wagerange status
gen rangestatus = _n 
sort rangestatus
replace rangestatus=rangestatus+1
set obs `=_N+1'
replace rangestatus = 1 if rangestatus==.
sort rangestatus

* setting graph locals
local ymin = -0.02
local ymax = 0.00
local ygap = 0.005

twoway (bar coeff rangestatus ,  bcolor(navy)) (rcap lower upper rangestatus , lcolor(navy) lpattern(shortdash)), ///
		xlab(2 "<p10" 4 "p10-20" 6 "p20-30" 8 "p30-40" 10 "p40-50" 12 "p50-60" 14 "p60-70" 16 "p70-80" 18 "p80-90" 20 "p90-95" 22 "p95-99" 24 "p99-100" ,angle(45)) xtitle("Employee earnings quantile") ylab(`ymin'(`ygap')`ymax', grid glcolor(gs15)) legend(off)  ///
		plotregion(color(white)) graphregion(color(white)) bgcolor(white) ysize(7) xsize(10) 
graph export $OUTPUT/fig_A6_panelC.png, replace


***********************
** Panel A - stay turnover decomp
***********************
import delimited "$DIR\figure_A6_panelA_turn.csv", rowrange(4:6) colrange(2:13) clear
gen year=2016
	
foreach i of numlist 1/10 {
	local j = `i'+1
	rename v`i' vv`j'
}

foreach i of numlist 2/11 {
	rename vv`i' v`i'
}	

foreach i of numlist 2/11 {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2
}
drop if coeff3 == .
keep year coeff* ste*

foreach i of numlist 2/11 {
	* generating upper and lower bound for CI
	gen upper`i' = coeff`i' + 1.96*ste`i' 
	gen lower`i' = coeff`i' - 1.96*ste`i' 
}

* reshape into long format
reshape long coeff ste lower upper, i(year) j(wagerange)
gen status=1

* generating space in between bars for better visulation in the figure 
* create a status = 4 to make more room between each wage range 
set obs `=_N*2'
replace status = 4 if status==.
replace wagerange=2 if status==4
replace wagerange=wagerange[_n-1]+1 if status==4 & year[_n-1]==. & wagerange==2
*for deciles
drop if wagerange>11
sort wagerange status
gen rangestatus = _n 
sort rangestatus

replace rangestatus=rangestatus+1
set obs `=_N+1'
replace rangestatus = 1 if rangestatus==.
sort rangestatus

gen samp = "turn"
tempfile tturn
save `tturn', replace


import delimited "$DIR\figure_A6_panelA_stay.csv", rowrange(4:6) colrange(2:13) clear
gen year=2016
	
foreach i of numlist 1/10 {
	local j = `i'+1
	rename v`i' vv`j'
}

foreach i of numlist 2/11 {
	rename vv`i' v`i'
}	

foreach i of numlist 2/11 {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2
}
drop if coeff3 == .
keep year coeff* ste*
	
foreach i of numlist 2/11 {
	* generating upper and lower bound for CI
	gen upper`i' = coeff`i' + 1.96*ste`i' 
	gen lower`i' = coeff`i' - 1.96*ste`i' 
}
reshape long coeff ste lower upper, i(year) j(wagerange)	
gen status=1

* generating space in between bars for better visulation in the figure 
* create a status = 4 to make more room between each wage range 
set obs `=_N*2'
replace status = 4 if status==.
replace wagerange=2 if status==4
replace wagerange=wagerange[_n-1]+1 if status==4 & year[_n-1]==. & wagerange==2
*for deciles
drop if wagerange>11
sort wagerange status
gen rangestatus = _n 
sort rangestatus

replace rangestatus=rangestatus+1
set obs `=_N+1'
replace rangestatus = 1 if rangestatus==.
sort rangestatus

gen samp = "stay"
tempfile tstay
save `tstay', replace

		
import delimited "$DIR\figure5_panelA.csv", rowrange(4:6) colrange(2:11) clear
gen year=2016

foreach i of numlist 1/10 {
	local j = `i'+1
	rename v`i' vv`j'
}

foreach i of numlist 2/11 {
	rename vv`i' v`i'
}	

foreach i of numlist 2/11 {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2
}
drop if coeff3 == .
keep year coeff* ste*

foreach i of numlist 2/11 {
	* generating upper and lower bound for CI
	gen upper`i' = coeff`i' + 1.96*ste`i' 
	gen lower`i' = coeff`i' - 1.96*ste`i' 
}

* reshape into long format
reshape long coeff ste lower upper, i(year) j(wagerange)
gen status=1

* generating space in between bars for better visulation in the figure 
* create a status = 4 to make more room between each wage range 
set obs `=_N*2'
replace status = 4 if status==.
replace wagerange=2 if status==4
replace wagerange=wagerange[_n-1]+1 if status==4 & year[_n-1]==. & wagerange==2

*for deciles
drop if wagerange>11

sort wagerange status
gen rangestatus = _n 
sort rangestatus

replace rangestatus=rangestatus+1
set obs `=_N+1'
replace rangestatus = 1 if rangestatus==.
sort rangestatus

gen samp = "all"

append using `tturn'
append using `tstay'

gen catturn = wagerange if samp=="turn"
replace catturn = catturn[_n-2]+5 if wagerange>2
gen catstay = wagerange+1 if samp=="stay"
replace catstay = catstay[_n-2]+5 if wagerange>2
gen catall = wagerange+2 if samp=="all"
replace catall = catall[_n-2]+5 if wagerange>2

twoway  (bar coeff catturn if status==1,  bcolor(cranberry)) (bar  coeff catstay if status==1,  bcolor(green))  (bar  coeff catall if status==1,  bcolor(navy)) ///
 (rcap lower upper catturn if status==1,  bcolor(cranberry)) (rcap lower upper catstay if status==1,  bcolor(green))  (rcap lower upper catall if status==1,  bcolor(navy)), ///
		xlab(3 "<p10" 8 "p10-20" 13 "p20-30" 18 "p30-40" 23 "p40-50" 28 "p50-60" 33 "p60-70" 38 "p70-80" 42 "p80-90" 48 "p90-100"  ,angle(45)) ///
		xtitle("Employee earnings decile") ylab(0(.005)-.02, grid glcolor(gs15)) ///
		legend(symxsize(*.5) region(lc(none) fc(none) ls(none)) keygap(*.25) colgap(*.25) region(lwidth(none)) size(small) order(1 "turnover" 2 "stayers" 3 "total") position(6) row(1) ring(0)) ///
		plotregion(color(white)) graphregion(color(white)) bgcolor(white) ysize(7) xsize(10) 
graph export $OUTPUT/fig_A6_panelA.png, replace



*************************
**  Stayers by baseline income bin
*************************
local cc "stay12"

import delimited "$DIR\figure_A6_panelB_stay12.csv", rowrange(4:6) colrange(2:11) clear
	gen year=2016

	foreach i of numlist 1/10 {
		local j = `i'+1
		rename v`i' vv`j'
	}

	foreach i of numlist 2/11 {
		rename vv`i' v`i'
	}	

	foreach i of numlist 2/11 {
		split v`i', gen(coef) parse(`"""', )
		gen coeff`i' = real(subinstr(coef2, "***", "", .))
		replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
		replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
		* clean up se 
		gen temp3 = subinstr(coef2, "(", "",.) 
		gen ste`i' = real(subinstr(temp3, ")", "",.) )
		replace ste`i' = ste`i'[_n+1]
drop temp3 coef1 coef2
}
drop if coeff3 == .
keep year coeff* ste*

foreach i of numlist 2/11 {
	* generating upper and lower bound for CI
	gen upper`i' = coeff`i' + 1.96*ste`i' 
	gen lower`i' = coeff`i' - 1.96*ste`i' 
}

* reshape into long format
reshape long coeff ste lower upper, i(year) j(wagerange)
gen status=1

* generating space in between bars for better visulation in the figure 
* create a status = 4 to make more room between each wage range 
set obs `=_N*2'
replace status = 4 if status==.
replace wagerange=2 if status==4
replace wagerange=wagerange[_n-1]+1 if status==4 & year[_n-1]==. & wagerange==2

*for deciles
drop if wagerange>11
sort wagerange status
gen rangestatus = _n 
sort rangestatus

replace rangestatus=rangestatus+1
set obs `=_N+1'
replace rangestatus = 1 if rangestatus==.
sort rangestatus

* setting graph locals
local ymin = -0.005
local ymax = 0.0025
local ygap = 0.0025

twoway (bar coeff rangestatus ,  bcolor(navy)) (rcap lower upper rangestatus , lcolor(navy) lpattern(shortdash)), ///
		xlab(2 "<p10" 4 "p10" 6 "p20" 8 "p30" 10 "p40" 12 "p50" 14 "p60" 16 "p70" 18 "p80" 20 "p90"  ,angle(45)) xtitle("Employee earnings quantile") ylab(`ymin'(`ygap')`ymax', grid glcolor(gs15)) legend(off) ///
		plotregion(color(white)) graphregion(color(white)) bgcolor(white) ysize(7) xsize(10) 
graph export $OUTPUT/fig_A6_panelB.png, replace

		
		


********************************************************************************
********************************************************************************
********************************************************************************
**  Figure A.7 - Liability Robustness
********************************************************************************
********************************************************************************
********************************************************************************

*******************************************************
**  Panel A - robustness - owngroups - avg 2011-2012 
*******************************************************
*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A7_avg.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

*save tempfile to append to other industries
gen cat="all"

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.02
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

*set specification
local zspec "1"
local spec = `zspec'+1

*****
* Event study figure *
*****
local ccat "all"
graph twoway (rcap lower`spec' upper`spec' `xvar'  if year>=`xvarmin' & year<=`xvarmax' & cat=="`ccat'",  lcolor(`secolor1') ) ///
			(scatter coeff`spec' `xvar' if year>=`xvarmin' & year<=`xvarmax' & cat=="`ccat'",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(small) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))	                          ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig_A7_panelA.png, replace


*******************************************************
**  Panel B - Robustness - net of business income
*******************************************************

*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A7_nbus.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

*save tempfile to append to other industries
gen cat="all"

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.02
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

*set specification
local zspec "1"
local spec = `zspec'+1

*****
* Event study figure *
*****
local ccat "all"
graph twoway (rcap lower`spec' upper`spec' `xvar'  if year>=`xvarmin' & year<=`xvarmax' & cat=="`ccat'",  lcolor(`secolor1') ) ///
			(scatter coeff`spec' `xvar' if year>=`xvarmin' & year<=`xvarmax' & cat=="`ccat'",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(small) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))	                          ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig_A7_panelB.png, replace

 

*******************************************************
**  Panel C - robustness - excluding top 01
*******************************************************
*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A7_top01.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

*save tempfile to append to other industries
gen cat="all"

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.02
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

*set specification
local zspec "1"
local spec = `zspec'+1

*****
* Event study figure *
*****
local ccat "all"
graph twoway (rcap lower`spec' upper`spec' `xvar'  if year>=`xvarmin' & year<=`xvarmax' & cat=="`ccat'",  lcolor(`secolor1') ) ///
			(scatter coeff`spec' `xvar' if year>=`xvarmin' & year<=`xvarmax' & cat=="`ccat'",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(small) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))	                          ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig_A7_panelC.png, replace



*******************************************************
**  Panel D - owngroups - net cap gains - K-1 liability
********************************************************
*set number of specs in table 
local ss = 1
local sp = `ss'+1
* import csv file and select first column only 
import delimited "$DIR\figure_A7_k1.csv", rowrange(4:27) colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

set obs `=_N+1'
replace year = 2012 if year == .
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

*save tempfile to append to other industries
gen cat="all"

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.02
local ygap = 0.01
local xvar year 
local xline = 2013.5 
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

*set specification
local zspec "1"
local spec = `zspec'+1

**
* Event study figure *
**
local ccat "all"
graph twoway (rcap lower`spec' upper`spec' `xvar'  if year>=`xvarmin' & year<=`xvarmax' & cat=="`ccat'",  lcolor(`secolor1')) ///
			(scatter coeff`spec' `xvar' if year>=`xvarmin' & year<=`xvarmax' & cat=="`ccat'",  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(small) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash))	                          ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig_A7_panelD.png, replace




********************************************************************************
********************************************************************************
********************************************************************************
**  Figure A.8 - Liability - single owner firms
********************************************************************************
********************************************************************************
********************************************************************************

*******************************************************
**  Panel A - reduced form all
*******************************************************
*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A8_panelA.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "1"
local spec1_1 = `zspec1_1'+1

local gtype "scatter"
**
* Event study figure *
**
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig_A8_panelA.png, replace 			

			
*********
**  Robust - single own - Top 
*********
*set number of specs in table 
local ss = 1
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A8_panelB.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

local lcolor1 "navy"
local secolor1 "gs8"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2009
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "1"
local spec1_1 = `zspec1_1'+1

local gtype "scatter"
**
* Event study figure *
**
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1')  ) ///
			(`gtype' coeff`spec1_1' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ), ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "Event-time coefficients")  order(2 ))			///
			xtitle("year") 			
graph export $OUTPUT/fig_A8_panelB.png, replace


	
*******************************************************
** Panel C - first stage
*******************************************************
* set rows - if base 2012: sr = 27 & srr=1 ;  if base 2011,2012: sr = 25 & srr=2
local sr = 25
local ss = 1
local sp = `ss'+1
local srr = 2

import delimited "$DIR\figure_A8_panelC.csv", rowrange(4:`sr') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

set obs `=_N+1'
replace year = 2012 if year == .
sort year 

gen vv=`srr'
if vv==2 {
set obs `=_N+1'
replace year = 2011 if year == .
sort year 
}
drop vv

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero = 0
gen pliab_tot = 29742
replace pliab_tot=0 if year<=2012

forval i=2/`sp' {
	gen zz = coeff`i' if year>2012
	egen mean`i'  = mean(zz)
	drop zz
	gen msh`i'=mean`i'/pliab_tot
}

local lcolor1 "navy"
local secolor1 "navy"
local lcolor2 "cranberry"
local secolor2 "cranberry"
local ymin = -00000
local ymax = 30000
local ygap = 10000
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2012
local xvarmax = 2016
local xgap = 1

*set specification
local xvarmin = 2011
local zspec = 1
local spec = `zspec'+1

local gtype "connected"

graph twoway (`gtype' pliab_tot `xvar'  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor2') lpattern(dash) msymbol(none) ) ///
			(`gtype' mean`spec' `xvar'  if year>=2013 & year<=`xvarmax' ,  lcolor(`secolor1') lpattern(dash) msymbol(none) ) ///
			(`gtype' coeff`spec' `xvar' if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) , ///
			graphregion(fcolor(white) icolor(white) color(white))                                 ///
			 xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' 2017))  ytitle("")  xtitle("year") ///
			xline(2012.3, lcolor(black) lpattern(dash)) ylab(`ymin'(`ygap')`ymax', nogrid)   ///
			 legend(symxsize(*.5) region(lc(none) fc(none) ls(none)) keygap(*.25) colgap(*.25) region(lwidth(none)) size(small) ring(0) position(5) col(1) order(1 "predicted increase in liability" 3 "estimated increase in liability" 2 "mean of estimated increase" )) ///
	       graphregion(color(white)) bgcolor(white)  ///
           ysize(7) xsize(10) ///
			text(29742 2016.2 "$29,742", place(e) color(sienna) size(small))  ///	
			text(24527 2016.2 "$24,527", place(e) color(navy) size(small)) 	
graph export $OUTPUT/fig_A8_panelC.png, replace
	   

	   


********************************************************************************
********************************************************************************
********************************************************************************
**  Fig A.9 - Robustness - loss firms first stage
********************************************************************************
********************************************************************************
********************************************************************************
import delimited "$DIR\figure_A9.csv", clear
		
local tt "tt1112"
graph twoway (connected sh year if trt==1 & tvc=="`tt'" & loss_2012==1, mcolor(navy) lcolor(navy)) /// 
			(connected sh year if trt==0 & tvc=="`tt'" & loss_2012==1, mcolor(cranberry) lcolor(cranberry)), /// 
			graphregion(fcolor(white) icolor(white) color(white))  plotregion(margin(medlarge)) ///
			ytitle("Fraction Treated") ///
			xline(2012.3, lcolor(black) lpattern(dash)) legend(col(2) order(1 "Basline Exposed Firms"  2 "Baseline Unexposed Firms"))
graph export "$OUTPUT/fig_A9.png", replace
		
				
				
	
			
********************************************************************************
********************************************************************************
********************************************************************************
**  Figure A.10 - control firms by exposure of CZ 
********************************************************************************
********************************************************************************
********************************************************************************
			
*set number of specs in table 
local ss = 2
local sp = `ss'+1
*set length (if base1112 = 24;  if base 2012 = 27)
local sl = 24

* import csv file and select first column only 
import delimited "$DIR\figure_A10_ge.csv", rowrange(4:`sl') colrange(:`sp') clear 

* clean up the numbers 
gen tempyear = regexs(0) if regexm(v1, "[0-9][0-9][0-9][0-9]")
destring tempyear, gen(year)

foreach i of numlist 2/`sp'  {
	split v`i', gen(coef) parse(`"""', )
	gen coeff`i' = real(subinstr(coef2, "***", "", .))
	replace coeff`i'  = real(subinstr(coef2, "**", "", .)) if coeff`i' ==.
	replace coeff`i'  = real(subinstr(coef2, "*", "", .)) if coeff`i' ==.
	* clean up se 
	gen temp3 = subinstr(coef2, "(", "",.) 
	gen ste`i' = real(subinstr(temp3, ")", "",.) )
	replace ste`i' = ste`i'[_n+1]
	drop temp3 coef1 coef2 
	}
drop if year == .
keep year coeff* ste*

*For base 2011-12
set obs `=_N+2'
gen zz=year==.
bys zz: gen zz7=_n
replace year = 2011 if zz==1 & zz7==1
replace year = 2012 if zz==1 & zz7==2
drop zz zz7
sort year 

foreach i of numlist 2/`sp' {
replace coeff`i' = 0 if coeff`i' ==.
}

foreach i of numlist 2/`sp' {
* generating upper and lower bound for CI
gen upper`i' = coeff`i' + 1.96*ste`i' 
gen lower`i' = coeff`i' - 1.96*ste`i' 
}

gen zero =0
gen year1 = year+.1
gen year2 = year-.1

local lcolor1 "navy"
local secolor1 "gs8"
local lcolor2 "cranberry"
local secolor2 "gs8"
local ymin = -.02
local ymax = 0.01
local ygap = 0.01
local xvar year 
local xline = 2013.5 // 0
local xvarmin = 2008
local xvarmax = 2016
local xgap = 1

*set specification
local zspec1_1 "1"
local spec1_1 = `zspec1_1'+1
local zspec2 "2"
local spec2 = `zspec2'+1

local gtype "scatter"
*
* Event study figure *
*
graph twoway (rcap lower`spec1_1' upper`spec1_1' `xvar'1  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor1') lpattern(dash) ) ///
			(`gtype' coeff`spec1_1' `xvar'1 if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor1') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor1') ) ///
			(rcap lower`spec2' upper`spec2' `xvar'2  if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`secolor2') lpattern(solid) ) ///
			(`gtype' coeff`spec2' `xvar'2 if year>=`xvarmin' & year<=`xvarmax' ,  lcolor(`lcolor2') lwidth(medium) msymbol(o) msize(medium) mcolor(`lcolor2') ), ///
			graphregion(fcolor(white) icolor(white) color(white))    xtitle("year")     ///
			plotregion(margin(medlarge)) xlabel(`xvarmin'(`xgap')`xvarmax') xscale(range(`xvarmin' `xvarmax'))	yline(0, lcolor(black)) xline(2012, lcolor(black) lpattern(dash)) ///
			legend(label(2 "25th v. 75th percentile") label(4 "20th v. 80th percentile") order(2 4) col(1))	
graph export $OUTPUT/fig_A10.png, replace
			
			
			
		
		




		